﻿angular.module('app.common.contactdetail', [])

    .controller('contactNoticeCtrl',
    ['$scope', 'custnum', 'invoiceIds', 'contactId', 'relatedEmail', 'contactPerson', '$modalInstance', 'contactProxy', 'collectorSoaProxy', 'baseDataProxy',
    function ($scope, custnum, invoiceIds, contactId, relatedEmail, contactPerson, $modalInstance, contactProxy, collectorSoaProxy, baseDataProxy) {
        var result = "";

        $scope.trackerList = [
                { "id": 1, "trackerName": 'Payment Notice Received' },
                { "id": 2, "trackerName": 'Contra' },
                { "id": 3, "trackerName": 'Breakdown' }
        ];

        if (relatedEmail) {
            $scope.relatedEmail = relatedEmail;
        }

        $scope.cancel = function () {
            result = "cancel";
            $modalInstance.close(result);
        };

        $scope.submit = function () {
            var result = [];
            result.push('submit');
            result.push($scope.trackStatus);
            result.push($scope.comss);

            var list = [];
            list.push('3');//3:notice
            list.push(invoiceIds.join(','));
            list.push(custnum);
            list.push(contactId);
            list.push(contactPerson);
            list.push($scope.trackStatus);
            list.push($("#comments").val());

            collectorSoaProxy.savecommon(list, function () {
                $modalInstance.close(result);
            })

            //for (i = 0; i < invoiceIds.length; i++) {
            //    contactProxy.insertDatasForNotice(custnum, invoiceIds[i], contactId, contactPerson, $("#comments").val(),
            //    function (res) {
            //        $modalInstance.close(result);
            //    }, function (error) {
            //        alert(error);
            //    })
            //}
        }
    } ])
//added by zhangYu
    .controller('contactCallCtrl',
    ['$scope', 'callInstance', 'custnum', 'invoiceIds', '$modalInstance', '$interval', 'contactCustomerProxy', 'modalService',
    function ($scope, callInstance, custnum, invoiceIds, $modalInstance, $interval, contactCustomerProxy, modalService) {
        $scope.callInstance = callInstance;
        if (callInstance.title == "Call Detail" || callInstance.title == "Call View") {
            var beginTime = new Date($scope.callInstance.beginTime);
            var endTime = new Date($scope.callInstance.endTime);
            $scope.callInstance.beginTime = beginTime.format("yyyy-MM-dd hh:mm");
            $scope.callInstance.endTime = endTime.format("yyyy-MM-dd hh:mm");
        }

        $interval(function () {
            $scope.setAttr();
        }, 0, 1);
        $scope.setAttr = function () {
            if (callInstance.title == "Call Create") {
                $("#btnSubmit").show();
                $("#btnSaveCall").hide();
                $("#txtContact").removeAttr("readonly");
                $("#imgContactor").hide();
                var now = new Date();
                $scope.callInstance.beginTime = now.format("yyyy-MM-dd hh:mm");
            }
            if (callInstance.title == "Call Detail") {
                $("#btnSaveCall").show();
                $("#btnSubmit").hide();
                $("#txtContact").removeAttr("readonly");
                $("#imgContactor").hide();
            }
            //invoice Log 
            if (callInstance.title == "Call View")
            {
                $("#btnSaveCall").hide();
                $("#btnSubmit").hide();
                $("#txtContact").removeAttr("readonly");
                $("#imgContactor").hide();
            }
            if (callInstance.title == "Dispute Call") {
                $("#btnSubmit").show();
                $("#btnSaveCall").hide();
                $("#imgContactor").show();
                $("#txtContact").removeAttr("readonly");
                var now = new Date();
                $scope.callInstance.beginTime = now.format("yyyy-MM-dd hh:mm:ss");
            }

        }

        $scope.pickupToAddress = function () {

            var contacterEmArr = new Array();
            // show contact list to pick up.
            var modalDefaults = {
                templateUrl: 'app/common/contactor/contactor-pickup.tpl.html',
                controller: 'contactorPickupCtrl',
                size: 'lg',
                resolve: {
                    custNum: function () { return custnum; },
                    contacterEmArr: function () { return contacterEmArr; },
                },
                windowClass: 'modalDialog'
            };

            modalService.showModal(modalDefaults).then(function (contacts) {
                var address = '';
                var collectorNames = '';
                angular.forEach(contacts, function (cont) {
                    if (!address) {
                        address += cont.emailAddress;
                    } else {
                        address += ';' + cont.emailAddress;
                    }
                    collectorNames += (cont.name + ', ');
                });

                $scope.callInstance.contacterId = address;
            });
        };

        $scope.submit = function () {
            if ($scope.callInstance.contacterId == null || $scope.callInstance.contacterId == "") {
                alert("Please input the contact");
                return;
            }
            if (callInstance.title == "Dispute Call") {
                if ($scope.callInstance.endTime == null) {
                    var now = new Date();
                    $scope.callInstance.endTime = now.format("yyyy-MM-dd hh:mm:ss");
                }
            } else {
                if ($scope.callInstance.endTime == null) {
                    var now = new Date();
                    $scope.callInstance.endTime = now.format("yyyy-MM-dd hh:mm");
                }
            }

            $scope.callInstance.invoiceIds = invoiceIds;
            $scope.callInstance.customerNum = custnum;
            $scope.callInstance.$save().then(function (result) {
                $modalInstance.close();
            })
        }
        

        $scope.closeWindow = function ()
        { $modalInstance.close(); }

    }])//contactCallCtrl End

    //contactBreakPTPCtrl Start
     .controller('contactBreakPTPCtrl',
    ['$scope', 'Instance', 'custnum', 'invoiceIds', '$modalInstance', 'breakPtpProxy', 'contactId',
    function ($scope, Instance, custnum, invoiceIds, $modalInstance, breakPtpProxy, contactId) {

        $scope.Instance = Instance;
        $scope.confirm = function () {
            if ($scope.Instance.discription == null || $scope.Instance.discription == "") {
                if (confirm("No Comments Input ! continue ?")) {

                }
                else {
                    return;
                }
            }

            $scope.Instance.invoiceIds = invoiceIds;
            $scope.Instance.customerNum = custnum;
            $scope.Instance.logAction = "BREAK PTP";
            $scope.Instance.logType = "6";
            $scope.Instance.newStatus = "004010";
            $scope.Instance.newTrack = "007";
            $scope.Instance.proofId = contactId;
            //$scope.Instance.discription = $scope.Instance.discription;
            breakPtpProxy.saveBreakPTP(Instance, function (res) {
                $modalInstance.close("submit");
            });
        }
        $scope.closeWindow = function ()
        { $modalInstance.close("cancel"); }

    }])//contactBreakPTPCtrl End
    //added by zhangYu

    //contactHoldCustomerCtrl Start
     .controller('contactHoldCustomerCtrl',
    ['$scope', 'Instance', 'custnum', 'invoiceIds', '$modalInstance', 'holdCustomerProxy', 'contactId',
    function ($scope, Instance, custnum, invoiceIds, $modalInstance, holdCustomerProxy, contactId) {

        $scope.Instance = Instance;
        $scope.confirm = function () {
            if ($scope.Instance.discription == null || $scope.Instance.discription == "") {
                if (confirm("No Comments Input ! continue ?")) {

                }
                else {
                    return;
                }
            }
                $scope.Instance.invoiceIds = invoiceIds;
                $scope.Instance.customerNum = custnum;
                $scope.Instance.logAction = "HOLD";
                $scope.Instance.logType = "8";//hold customer
                $scope.Instance.newStatus = "004011";
                $scope.Instance.newTrack = "011";
                $scope.Instance.proofId = contactId;
                //$scope.Instance.discription = $scope.Instance.discription;
                holdCustomerProxy.saveHoldCustomer(Instance, function (res) {
                    $modalInstance.close("submit");
                });
        }
        $scope.closeWindow = function ()
        { $modalInstance.close("cancel"); }

    }])//contactHoldCustomerCtrl End
    //added by zhangYu

    .controller('contactPtpCtrl',
    ['$scope', 'custnum', 'invoiceIds', 'contactId', 'relatedEmail', 'contactPerson', '$modalInstance', 'contactProxy', 'collectorSoaProxy',
    function ($scope, custnum, invoiceIds, contactId, relatedEmail, contactPerson, $modalInstance, contactProxy, collectorSoaProxy) {
        var result = "";
        $scope.promissDate = "";
        if (relatedEmail) {
            $scope.relatedEmail = relatedEmail;
        }

        $scope.cancel = function () {
            result = "cancel";
            $modalInstance.close(result);
        };

        $scope.submit = function () {
            if ($scope.promissDate == "") {
                alert("Please choose promiss date !");
                return;
            }
            //added by zhangYu part refrsh dunning invoice list Start
            var result=[]
            result.push('submit');// = "submit";
            result.push($("#comments").val());
            result.push($scope.promissDate);
            //added by zhangYu part refrsh dunning invoice list End
            var list = [];
            list.push('4');//4:ptp
            list.push(invoiceIds.join(','));
            list.push(custnum);
            list.push(contactId);
            list.push(contactPerson);
            list.push($("#comments").val());
            list.push($scope.promissDate);

            collectorSoaProxy.savecommon(list, function () {
                $modalInstance.close(result);
            })

            //for (i = 0; i < invoiceIds.length; i++) {
            //    contactProxy.insertDatasForPtp(custnum, invoiceIds[i], contactId, contactPerson, $("#comments").val(), $scope.promissDate,
            //    function (res) {
            //        $modalInstance.close(result);
            //    }, function (error) {
            //        alert(error);
            //    })
            //}
        }
    } ])

    .controller('contactDisputeCtrl',
    ['$scope', 'disInvInstance', 'custnum', 'invoiceIds', 'contactId', 'relatedEmail', 'contactPerson', '$modalInstance', 'contactCustomerProxy', 'baseDataProxy',
    function ($scope, disInvInstance, custnum, invoiceIds, contactId, relatedEmail, contactPerson, $modalInstance, contactCustomerProxy, baseDataProxy) {
        var result = "";
        $scope.disInvInstance = disInvInstance;
        baseDataProxy.SysTypeDetail("025", function (list) {
            $scope.disputeReasonList = list;
        })

        if (relatedEmail) {
            $scope.relatedEmail = relatedEmail;
        }

        $scope.cancel = function () {
            result = "cancel";
            $modalInstance.close(result);
        };

        $scope.submit = function () {
            if ($scope.issue == "undefined" || $scope.issue == null) {
                alert("Please choose an issue reason !");
                return;
            }
            result = "submit";
            
            $scope.disInvInstance.customerNum = custnum;
            $scope.disInvInstance.invoiceIds = invoiceIds;
            $scope.disInvInstance.contactId = contactId;
            $scope.disInvInstance.relatedEmail = relatedEmail;
            $scope.disInvInstance.contactPerson = contactPerson;
            $scope.disInvInstance.comments = $("#comments").val();
            $scope.disInvInstance.issue = $scope.issue;

            $scope.disInvInstance.$save(function (res) {
                $modalInstance.close(result);
            }, function (err) {
                alert(err);
            })
        }

    } ])